package BinarySearchTree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-28
 * Time: 17:20
 */


public class MyBinarySearchTree {

     static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

         public TreeNode() {

         }

        public TreeNode(int val) {
            this.val = val;
        }


    }
    public TreeNode root;



    //插入
    public void insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return;
        }

        TreeNode cur = root;
        TreeNode node = new TreeNode(key);
        TreeNode parent = null;

        //找到要插入的位置
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else {
                //搜索二叉树，不会插入相同的节点
                return;
            }
        }
        //插入
        if (parent.val > key) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }


    /**
     * 搜索：
     * 时间复杂度：最坏情况 O(N),  最好情况：O(logN)
     * @param key
     * @return
     */
    public TreeNode search(int key) {

        TreeNode cur = root;
        if (cur == null) {
            return null;
        }

        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            }else if (cur.val < key) {
                cur = cur.right;
            }else {
                return cur;
            }
        }
        return null;
    }


    //删除
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;

        //找到要删除的结点
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else {
                removeNode(parent,cur);
                return;
            }
        }
    }

    /**删除有三种情况：
     *   1.cur的左边为空
     *   2.cur的右边为空
     *   1.cur的左右两边都不为空
     */
    private void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            }else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }

        }else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            }else if (cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode tp = cur;
            TreeNode t = cur.right;
            while (t.left != null) {
                tp = t;
                t = t.left;
            }

            cur.val = t.val;//交换

            //删除
            if (tp.left == t) {
                tp.left = t.right;
            }else {
                tp.right = t.right;
            }
        }

    }
}
